{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Using-Workspace-and-Projects\" data-toc-modified-id=\"Using-Workspace-and-Projects-1\">Using Workspace and Projects</a></span><ul class=\"toc-item\"><li><span><a href=\"#Usage-of-Projects-and-Workspace\" data-toc-modified-id=\"Usage-of-Projects-and-Workspace-1.1\">Usage of Projects and Workspace</a></span></li><li><span><a href=\"#Design-of-Workspace-and-Project-Classes\" data-toc-modified-id=\"Design-of-Workspace-and-Project-Classes-1.2\">Design of Workspace and Project Classes</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using Workspace and Projects\n",
    "\n",
    "OpenPNM V2.0 implemented new ways to manage and control objects, specifically the *Workspace* and *Projects*. \n",
    "\n",
    "> You probably don't need to worry about these features until your simulations get large and complicated.\n",
    "\n",
    "The *Workspace* is equivalent to a web browser window, while a *Project* is like tabs inside the browser. Each *Project* is an isolated OpenPNM simulation with a single *Network* and all associated objects.  All *Projects* are stored in the same *Workspace*.  There can be only 1 *Workspace* open at a given time, so all new projects are registered in the same *Workspace*.  *Projects* and *Workspaces* can be saved and loaded.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.232913Z",
     "iopub.status.busy": "2021-06-24T11:25:16.231307Z",
     "iopub.status.idle": "2021-06-24T11:25:16.814306Z",
     "shell.execute_reply": "2021-06-24T11:25:16.812842Z"
    }
   },
   "outputs": [],
   "source": [
    "import openpnm as op"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Usage of Projects and Workspace\n",
    "\n",
    "Initialize the *Workspace* and save in a variable called ``ws``, and print it to verify that it is currently empty:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.820923Z",
     "iopub.status.busy": "2021-06-24T11:25:16.819453Z",
     "iopub.status.idle": "2021-06-24T11:25:16.827591Z",
     "shell.execute_reply": "2021-06-24T11:25:16.826509Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "OpenPNM Version 2.7.0 Workspace\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    
    "ws = op.Workspace()\n",
    "print(ws)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, create a new *Project* and print:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.843509Z",
     "iopub.status.busy": "2021-06-24T11:25:16.842063Z",
     "iopub.status.idle": "2021-06-24T11:25:16.848110Z",
     "shell.execute_reply": "2021-06-24T11:25:16.849258Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "proj = ws.new_project()\n",
    "proj"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The project is an empty list since there have been no objects created yet.  \n",
    "\n",
    "Now create a new network object with passing in ``proj`` into the initialization:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.860066Z",
     "iopub.status.busy": "2021-06-24T11:25:16.858649Z",
     "iopub.status.idle": "2021-06-24T11:25:16.862976Z",
     "shell.execute_reply": "2021-06-24T11:25:16.861778Z"
    }
   },
   "outputs": [],
   "source": [
    "pn = op.network.Cubic(shape=[4, 4, 4], project=proj)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now printing the *Project* via `print(proj)` will include the newly created network:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.869809Z",
     "iopub.status.busy": "2021-06-24T11:25:16.868559Z",
     "iopub.status.idle": "2021-06-24T11:25:16.873139Z",
     "shell.execute_reply": "2021-06-24T11:25:16.874138Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " Object Name     Object ID                                                        \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " net_01          <openpnm.network.Cubic object at 0x7f93080664f0>                 \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    
    "print(proj)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is not necessary to create a project *before* creating a network.  Since a project can only have *one* network, then a new project is created each time a network is created if not specified as we did above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.882651Z",
     "iopub.status.busy": "2021-06-24T11:25:16.881684Z",
     "iopub.status.idle": "2021-06-24T11:25:16.891640Z",
     "shell.execute_reply": "2021-06-24T11:25:16.890466Z"
    }
   },
   "outputs": [],
   "source": [
    "pn2 = op.network.Cubic(shape=[4, 4, 4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The project that each object belongs to can be found since each object has a ``project`` attribute. You can use this to see what other objects are defined in a project: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.898855Z",
     "iopub.status.busy": "2021-06-24T11:25:16.897462Z",
     "iopub.status.idle": "2021-06-24T11:25:16.902272Z",
     "shell.execute_reply": "2021-06-24T11:25:16.903431Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " Object Name     Object ID                                                        \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " net_01          <openpnm.network.Cubic object at 0x7f9308066680>                 \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    
    "print(pn2.project)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, you can view all active projects by printing the workspace via:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.911258Z",
     "iopub.status.busy": "2021-06-24T11:25:16.909807Z",
     "iopub.status.idle": "2021-06-24T11:25:16.915658Z",
     "shell.execute_reply": "2021-06-24T11:25:16.914634Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "OpenPNM Version 2.7.0 Workspace\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " proj_01\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " Object Name     Object ID                                                        \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " net_01          <openpnm.network.Cubic object at 0x7f93080664f0>                 \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " proj_02\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " Object Name     Object ID                                                        \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " net_01          <openpnm.network.Cubic object at 0x7f9308066680>                 \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    
    "print(ws)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A project can be purged from the workspace via `ws.close_project(proj)`. Let's print workspace again,:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-06-24T11:25:16.923204Z",
     "iopub.status.busy": "2021-06-24T11:25:16.922248Z",
     "iopub.status.idle": "2021-06-24T11:25:16.927033Z",
     "shell.execute_reply": "2021-06-24T11:25:16.927981Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "OpenPNM Version 2.7.0 Workspace\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " proj_02\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " Object Name     Object ID                                                        \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      " net_01          <openpnm.network.Cubic object at 0x7f9308066680>                 \n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    
    "ws.close_project(proj)\n",
    "print(ws)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Design of Workspace and Project Classes\n",
    "\n",
    "It is worth describing the design of these classes to help explain how they work.  The workspace is a ``dict`` subclass while the project is a ``list`` subclass.  Each subclass has numerous methods added to aid in the management of the objects. \n",
    "\n",
    "In the Workspace ``dict`` each project object is stored by name in each 'key: value' pair.  Printing the workspace gives a nicely formatted output as shown above, but the basic command-line representation gives its true structure via running `ws` in the command-line:"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "{'sim_02': [<openpnm.network.Cubic object at 0x7f1fa5700830>]}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where the only project in the workspace is ``'sim_02'``, and it is a list that contains only a single Cubic network object."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
